import { IAvailableTabbarPath, tabbarPages } from "@/constants/pageMap"
import { setActive } from "@/store/actions/tabbar"
import { withStore as useStore } from "@/store/index"

/** 判断给定路径是否是tabbar页面 */
export const isTabbarPage = (pagePath: string): string | null => {
  const tabbars = [...Object.values(tabbarPages)]
  const index = tabbars.findIndex(tab => pagePath.includes(tab))
  if (index !== -1) {
    return tabbars[index]
  }
  return null
}
/**
 * @description 页面onShow需要执行此方法同步tabbar选中状态，解决从分享卡片等途径进入小程序时点击分享页左上角按钮后tabbar状态同步的问题
 */
export const syncTabbarActive = (tabbarPagePath: IAvailableTabbarPath) => {
  const store = useStore()
  if (store.getState().tabbar.current !== tabbarPagePath) {
    store.dispatch(setActive(tabbarPagePath))
  }
}
